home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Programming / MuManual / C_Sources / MuScan.c < prev    next >
C/C++ Source or Header  |  1999-11-28  |  16KB  |  386 lines

  1. /*************************************************************************
  2.  ** mmu.library                                                         **
  3.  **                                                                     **
  4.  ** a system library for arbitration and control of the MC68K MMUs      **
  5.  **                                                                     **
  6.  ** © 1998,1999 THOR-Software, Thomas Richter                           **
  7.  ** No commercial use, reassembly, modification without prior, written  **
  8.  ** permission of the authors.                                          **
  9.  ** Including this library in any commercial software REQUIRES a        **
  10.  ** written permission and the payment of a small fee.                  **
  11.  **                                                                     **
  12.  ** This sample source demonstrates how to print out the mapping of the **
  13.  ** MMU library. It's here done for the default context                 **
  14.  *************************************************************************/
  15.  
  16. /// Includes
  17. #include <exec/types.h>
  18. #include <exec/memory.h>
  19. #include <dos/dos.h>
  20. #include <mmu/mmubase.h>
  21. #include <mmu/context.h>
  22. #include <mmu/config.h>
  23. #include <workbench/startup.h>
  24.  
  25. #include <proto/exec.h>
  26. #include <proto/mmu.h>
  27. #include <proto/dos.h>
  28. #include <proto/icon.h>
  29.  
  30. #include <string.h>
  31. ///
  32. /// Defines
  33. #define STRINGDATE "28.11.99"
  34. #define STRINGVERSION "40.3"
  35. #define TEMPLATE "TO/K"
  36.  
  37. #define OPT_TO  0
  38. #define OPT_WINDOW 1
  39. #define OPT_COUNT 2
  40. ///
  41. /// Statics
  42. struct MMUBase *MMUBase;
  43. struct DosLibrary *DOSBase;
  44. struct ExecBase *SysBase;
  45. struct Library *IconBase;
  46. ///
  47. /// Protos
  48. int __asm __saveds main(void);
  49. void ScanMMU(BPTR to);
  50. struct RDArgs *ReadTTArgs(struct WBStartup *msg,LONG args[],struct RDArgs **tmp);
  51. ///
  52.  
  53. char version[]="$VER: MuScan " STRINGVERSION " (" STRINGDATE ") © THOR";
  54.  
  55. /// main
  56. int __asm __saveds main(void)
  57. {
  58. LONG args[OPT_COUNT];
  59. struct RDArgs *rd,*myrd;
  60. struct Process *proc;
  61. int rc=20;
  62. LONG err;
  63. struct WBStartup *msg;
  64. BPTR oldout;
  65. struct MsgPort *oldconsole;
  66. BPTR out;
  67.  
  68.  
  69.         SysBase=*((struct ExecBase **)(4L));
  70.  
  71.         memset(args,0,sizeof(LONG)*OPT_COUNT);
  72.         /* Wait for the workbench startup, if any */
  73.         proc=(struct Process *)FindTask(NULL);
  74.  
  75.         if (!(proc->pr_CLI)) {
  76.                 WaitPort(&(proc->pr_MsgPort));
  77.                 msg=(struct WBStartup *)GetMsg(&(proc->pr_MsgPort));
  78.         } else  msg=NULL;
  79.  
  80.         if (DOSBase=(struct DosLibrary *)OpenLibrary("dos.library",37L)) {
  81.                 if (MMUBase=(struct MMUBase *)OpenLibrary("mmu.library",40L)) {
  82.  
  83.                         myrd=NULL;      /* reset the temporary ReadArgs */
  84.                         oldout=NULL;
  85.                         oldconsole=NULL;
  86.                         if (msg) {
  87.                                 oldout=SelectOutput(NULL);
  88.                                 oldconsole=SetConsoleTask(NULL);
  89.                                 rd=ReadTTArgs(msg,args,&myrd);
  90.                         } else  rd=ReadArgs(TEMPLATE,args,NULL);
  91.  
  92.                         if (rd) {
  93.                                 /* Argument parser worked, call main routine */
  94.                                 if (args[OPT_TO])       out=Open((char *)(args[0]),MODE_NEWFILE);
  95.                                 else                    out=Output();
  96.  
  97.                                 if (out) {
  98.                                         ScanMMU(out);
  99.                                         err=0;
  100.                                         if (args[OPT_TO])
  101.                                                 Close(out);
  102.                                 } else err=IoErr();
  103.  
  104.                                 FreeArgs(rd);
  105.                                 if (myrd) FreeDosObject(DOS_RDARGS,myrd);
  106.                                 if (msg)  Close(SelectOutput(NULL));
  107.                         } else  err=IoErr();
  108.  
  109.                         if (msg) {
  110.                                 SelectOutput(oldout);
  111.                                 SetConsoleTask(oldconsole);
  112.                         }
  113.  
  114.                         if (err<64) {
  115.                                 rc=err;
  116.                                 err=0;
  117.                         } else {
  118.                                 if (!msg) PrintFault(err,"MuScan failed");
  119.                                 rc=10;
  120.                         }
  121.                         SetIoErr(err);
  122.  
  123.                         CloseLibrary((struct Library *)MMUBase);
  124.                 } else PrintFault(ERROR_OBJECT_NOT_FOUND,"MuScan requires the mmu.library V40 or better");
  125.                 CloseLibrary((struct Library *)DOSBase);
  126.         }
  127.  
  128.         if (msg) {
  129.                 Forbid();
  130.                 ReplyMsg((struct Message *)msg);
  131.         }
  132.  
  133.         return rc;
  134. }
  135. ///
  136. /// ReadTTArgs
  137. struct RDArgs *ReadTTArgs(struct WBStartup *msg,LONG args[],struct RDArgs **tmp)
  138. {
  139. struct WBArg *wbarg;
  140. struct DiskObject *dop;
  141. char **tt;                      /* ToolTypes array */
  142. char *wbstr;                    /* Our self-made workbench argument string */
  143. char *here;
  144. BPTR oldlock;
  145. ULONG len;
  146. struct RDArgs *rd=NULL,*myrd=NULL;
  147. LONG err=0;
  148. BPTR newout;
  149.  
  150.         if (IconBase=OpenLibrary("icon.library",37L)) {
  151.                 if (wbarg=msg->sm_ArgList) {
  152.                         /* use a project icon if there is one... */
  153.                         if (msg->sm_NumArgs > 1) wbarg++;
  154.  
  155.                         /* go into the directory */
  156.                         oldlock=CurrentDir(wbarg->wa_Lock);
  157.  
  158.                         if (dop=GetDiskObject(wbarg->wa_Name)) {
  159.                                 if (tt=dop->do_ToolTypes) {
  160.                                         /* Read a special tool type for the output window */
  161.  
  162.                                         /* Calc the size of the argument string */
  163.  
  164.                                         len = 3;        /* reserve space for SPC,LF,NUL */
  165.                                         while (*tt) {
  166.                                                 len += strlen(*tt)+1;   /* string, plus space */
  167.                                                 tt++;
  168.                                         }
  169.  
  170.                                         if (wbstr=AllocVec(len,MEMF_PUBLIC)) {
  171.                                                 /* Now copy the arguments into this string, one by one
  172.                                                    and check whether the argument string is still valid. */
  173.  
  174.                                                 tt=dop->do_ToolTypes;
  175.                                                 here=wbstr;
  176.                                                 do{
  177.                                                         *here='\0';                     /* terminate string */
  178.                                                         /* Check whether this tool type is
  179.                                                            commented out. Just ignore it in this case */
  180.                                                         if (*tt) {
  181.                                                                 if (**tt=='(' || **tt==';')
  182.                                                                         continue;
  183.  
  184.                                                                 strcpy(here,*tt);      /* Add TT string */
  185.                                                         }
  186.                                                         len=strlen(here);
  187.                                                         here[len]='\n';
  188.                                                         here[len+1]='\0';               /* terminate string */
  189.  
  190.                                                         /* Now try to ReadArg' this string */
  191.  
  192.                                                         /* release old arguments left over from last loop */
  193.                                                         if (rd) FreeArgs(rd);
  194.                                                         if (myrd) FreeDosObject(DOS_RDARGS,myrd);
  195.                                                         rd=NULL;
  196.                                                         memset(args,0,sizeof(LONG)*OPT_COUNT);
  197.  
  198.                                                         if (myrd=AllocDosObject(DOS_RDARGS,NULL)) {
  199.                                                                 /* Allocate and setup the ReadArgs source */
  200.                                                                 myrd->RDA_Source.CS_Buffer=wbstr;
  201.                                                                 myrd->RDA_Source.CS_Length=strlen(wbstr);
  202.  
  203.                                                                 if (rd=ReadArgs(TEMPLATE ",WINDOW/K",args,myrd)) {
  204.                                                                         /* Is this still valid? */
  205.                                                                         here[len]=' ';
  206.                                                                         here+=len+1;
  207.                                                                         /* if so, accept this argument and go on */
  208.                                                                 } else {
  209.                                                                         err=IoErr();
  210.                                                                         if (err==ERROR_NO_FREE_STORE) break;
  211.                                                                         else    err=0;  /* Ignore unknown or invalid arguments silently */
  212.                                                                 }
  213.                                                         } else {
  214.                                                                 err=ERROR_NO_FREE_STORE;
  215.                                                                 break;
  216.                                                         }
  217.                                                 }while(*tt++);
  218.  
  219.                                                 FreeVec(wbstr);
  220.                                         } else err=ERROR_NO_FREE_STORE;
  221.                                 } else err=ERROR_REQUIRED_ARG_MISSING; /* Huh, how should this happen ? */
  222.                                 FreeDiskObject(dop);
  223.                         } else err=IoErr();
  224.                         CurrentDir(oldlock);
  225.                 } else err=ERROR_REQUIRED_ARG_MISSING; /* This should not happen either */
  226.                 CloseLibrary(IconBase);
  227.         } else err=ERROR_OBJECT_NOT_FOUND;    /* This should not happen */
  228.  
  229.         /* Open an output stream */
  230.  
  231.         if (err==0) {
  232.                 if (newout=Open((args[OPT_WINDOW])?((char *)args[OPT_WINDOW]):("NIL:"),MODE_NEWFILE)) {
  233.                         SelectOutput(newout);
  234.                         /* Hack in the output console. Well, well... */
  235.                         SetConsoleTask(((struct FileHandle *)(BADDR(newout)))->fh_Type);
  236.                 } else err=IoErr();
  237.         }
  238.  
  239.         if (err) {
  240.                 if (rd)   FreeArgs(rd);
  241.                 if (myrd) FreeDosObject(DOS_RDARGS,myrd);
  242.                 SetIoErr(err);
  243.                 rd=NULL;
  244.                 myrd=NULL;
  245.         }
  246.  
  247.         *tmp=myrd;
  248.         return rd;
  249. }
  250. ///
  251. /// ScanMMU
  252. void ScanMMU(BPTR to)
  253. {
  254. struct MinList *list;
  255. struct MappingNode *mn;
  256.  
  257.         FPrintf(to,version+6);
  258.         FPrintf(to,"\n\n");
  259.  
  260.         switch (GetMMUType()) {
  261.                 case MUTYPE_NONE:
  262.                         FPrintf(to,"No MMU available.\n");
  263.                         break;
  264.                 case MUTYPE_68851:
  265.                         FPrintf(to,"68851 MMU detected.\n");
  266.                         break;
  267.                 case MUTYPE_68030:
  268.                         FPrintf(to,"68030 MMU detected.\n");
  269.                         break;
  270.                 case MUTYPE_68040:
  271.                         FPrintf(to,"68040 MMU detected.\n");
  272.                         break;
  273.                 case MUTYPE_68060:
  274.                         FPrintf(to,"68060 MMU detected.\n");
  275.                         break;
  276.         };
  277.  
  278.         /* Read now the page size of the MMU for the default
  279.            (public) context */
  280.  
  281.         FPrintf(to,"MMU page size is 0x%lx bytes.\n\n",GetPageSize(NULL));
  282.  
  283.  
  284.         /* Get the mapping of the default context */
  285.         list=GetMapping(NULL);
  286.  
  287.         /* next, we print it out. Usually, you should
  288.            make a copy first since the context is now
  289.            blocked, i.e. nobody else will be allowed to
  290.            make changes to it. */
  291.  
  292.         FPrintf(to,"Memory map:\n");
  293.  
  294.         for(mn=(struct MappingNode *)(list->mlh_Head);mn->map_succ;mn=mn->map_succ) {
  295.  
  296.                 FPrintf(to,"0x%08lx - 0x%08lx ",mn->map_Lower,mn->map_Higher);
  297.  
  298.                 if (mn->map_Properties & MAPP_WRITEPROTECTED)
  299.                         FPrintf(to,"WriteProtected ");
  300.  
  301.                 if (mn->map_Properties & MAPP_USED)
  302.                         FPrintf(to,"U ");
  303.  
  304.                 if (mn->map_Properties & MAPP_MODIFIED)
  305.                         FPrintf(to,"M ");
  306.  
  307.                 if (mn->map_Properties & MAPP_GLOBAL)
  308.                         FPrintf(to,"Global ");
  309.  
  310.                 if (mn->map_Properties & MAPP_TRANSLATED)
  311.                         FPrintf(to,"TT ");
  312.  
  313.                 if (mn->map_Properties & MAPP_ROM)
  314.                         FPrintf(to,"ROM ");
  315.  
  316.                 if (mn->map_Properties & MAPP_USERPAGE0)
  317.                         FPrintf(to,"UP0 ");
  318.  
  319.                 if (mn->map_Properties & MAPP_USERPAGE1)
  320.                         FPrintf(to,"UP1 ");
  321.  
  322.                 if (mn->map_Properties & MAPP_CACHEINHIBIT)
  323.                         FPrintf(to,"CacheInhibit ");
  324.  
  325.                 if (mn->map_Properties & MAPP_IMPRECISE)
  326.                         FPrintf(to,"Imprecise ");
  327.  
  328.                 if (mn->map_Properties & MAPP_NONSERIALIZED)
  329.                         FPrintf(to,"NonSerial ");
  330.  
  331.                 if (mn->map_Properties & MAPP_COPYBACK)
  332.                         FPrintf(to,"CopyBack ");
  333.  
  334.                 if (mn->map_Properties & MAPP_SUPERVISORONLY)
  335.                         FPrintf(to,"SuperOnly ");
  336.  
  337.                 if (mn->map_Properties & MAPP_BLANK)
  338.                         FPrintf(to,"Blank ");
  339.  
  340.                 if (mn->map_Properties & MAPP_SHARED)
  341.                         FPrintf(to,"Shared ");
  342.  
  343.                 if (mn->map_Properties & MAPP_SINGLEPAGE)
  344.                         FPrintf(to,"Single ");
  345.  
  346.                 if (mn->map_Properties & MAPP_REPAIRABLE)
  347.                         FPrintf(to,"Repairable ");
  348.  
  349.                 if (mn->map_Properties & MAPP_IO)
  350.                         FPrintf(to,"I/O space ");
  351.  
  352.                 if (mn->map_Properties & MAPP_USER0)
  353.                         FPrintf(to,"U0 ");
  354.  
  355.                 if (mn->map_Properties & MAPP_USER1)
  356.                         FPrintf(to,"U1 ");
  357.  
  358.                 if (mn->map_Properties & MAPP_USER2)
  359.                         FPrintf(to,"U2 ");
  360.  
  361.                 if (mn->map_Properties & MAPP_USER3)
  362.                         FPrintf(to,"U3 ");
  363.  
  364.                 if (mn->map_Properties & MAPP_INVALID)
  365.                         FPrintf(to,"Invalid (0x%08lx) ",mn->map_un.map_UserData);
  366.  
  367.                 if (mn->map_Properties & MAPP_SWAPPED)
  368.                         FPrintf(to,"Swapped (0x%08lx) ",mn->map_un.map_UserData);
  369.  
  370.                 if (mn->map_Properties & MAPP_REMAPPED)
  371.                         FPrintf(to,"Remapped to 0x%08lx ",mn->map_un.map_Delta+mn->map_Lower);
  372.  
  373.                 if (mn->map_Properties & MAPP_BUNDLED)
  374.                         FPrintf(to,"Bundled to 0x%08lx ",mn->map_un.map_Page);
  375.  
  376.                 if (mn->map_Properties & MAPP_INDIRECT)
  377.                         FPrintf(to,"Indirect at 0x%08lx ",mn->map_un.map_Descriptor);
  378.  
  379.                 FPrintf(to,"\n");
  380.         }
  381.  
  382.         ReleaseMapping(NULL,list);
  383. }
  384. ///
  385.  
  386.